/**
 * 
 */
package de.mbenning.fsim.persistence.dao;

import java.util.List;

import org.hibernate.ObjectNotFoundException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.mbenning.fsim.domain.Kontinent;
import de.mbenning.fsim.domain.Land;
import de.mbenning.fsim.domain.Liga;
import de.mbenning.fsim.persistence.dao.interfaces.IMainDAO;

/**
 * @author Martin.Benning
 *
 */
public class MainDAO extends HibernateDaoSupport implements IMainDAO {
	
	public void store(Object object) {
		getHibernateTemplate().saveOrUpdate(object);
		getHibernateTemplate().flush();
	}

	public void delete(Object object) {
		getHibernateTemplate().delete(object);
		getHibernateTemplate().flush();
	}

	public void update(Object object) {
		getHibernateTemplate().update(object);
		getHibernateTemplate().flush();
	}

	public void insert(Object object) {
		getHibernateTemplate().save(object);	
		getHibernateTemplate().flush();
	}
	
	public Kontinent getKontinentById(Integer id) {
		for (final Object object : getHibernateTemplate().find(" FROM Kontinent A WHERE A.id = ?", id)) {
            return (Kontinent) object;
        }
        throw new ObjectNotFoundException("Kontinent", Kontinent.class.getName());
	}

	public Kontinent getKontinentByName(String name) {
		for (final Object object : getHibernateTemplate().find(" FROM Kontinent A WHERE A.name = ?", name)) {
            return (Kontinent) object;
        }
        throw new ObjectNotFoundException("Kontinent", Kontinent.class.getName());
	}

	public Land getLandById(Integer id) {
		for (final Object object : getHibernateTemplate().find(" FROM Land A WHERE A.id = ?", id)) {
            return (Land) object;
        }
        throw new ObjectNotFoundException("Land", Land.class.getName());
	}

	public Land getLandByName(String name) {
		for (final Object object : getHibernateTemplate().find(" FROM Land A WHERE A.name = ?", name)) {
            return (Land) object;
        }
        throw new ObjectNotFoundException("Land", Land.class.getName());
	}
	
	@SuppressWarnings("unchecked")
	public List<Land> findAllLaender() {
		return getHibernateTemplate().find(" FROM Land A ");
	}
	
	public Liga getLigaByName(String name) {
		for (final Object object : getHibernateTemplate().find(" FROM Liga A WHERE A.name = ?", name)) {
            return (Liga) object;
        }
        throw new ObjectNotFoundException("Liga", Liga.class.getName());
	}
	
	public Liga getLigaById(Integer id) {
		for (final Object object : getHibernateTemplate().find(" FROM Liga A WHERE A.id = ?", id)) {
            return (Liga) object;
        }
        throw new ObjectNotFoundException("Liga", Liga.class.getName());
	}

	@SuppressWarnings("unchecked")
	public List<Kontinent> findAllKontinent() {
		return getHibernateTemplate().find(" FROM Kontinent A ORDER BY A.name ");
	}

	public List<Liga> findAllLigen() {
		return getHibernateTemplate().find(" FROM Liga A ORDER BY A.land.name, A.name ");
	}
	
}
